﻿// Copyright (c) BeiYinZhiNian (1031622947@qq.com). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: http://www.caviar.wang/ or https://gitee.com/Cherryblossoms/caviar.

using Caviar.AntDesignUI;
using Caviar.Demo.Wasm;
using Caviar.Core.Services;
using Caviar.Infrastructure;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Builder;
using Caviar.SharedKernel.Entities;

var builder = WebApplication.CreateBuilder(args);
var services = builder.Services;
var configuration = builder.Configuration;

builder.Services.AddRazorComponents()
    .AddInteractiveServerComponents()
    .AddInteractiveWebAssemblyComponents();

services.Configure<IdentityOptions>(options =>
{
    options.Password.RequireDigit = false;
    options.Password.RequireUppercase = false;
    options.Password.RequireLowercase = false;
    options.Password.RequireNonAlphanumeric = false;

    options.Lockout.AllowedForNewUsers = false;
    options.Lockout.MaxFailedAccessAttempts = 10;
    options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(1);

    options.User.RequireUniqueEmail = true;
});
//cookies配置
services.ConfigureApplicationCookie(options =>
{
    options.Cookie.HttpOnly = true;
    options.ExpireTimeSpan = TimeSpan.FromMinutes(60);
});
//cookies验证配置
services
    .AddAuthentication(cfg =>
    {
        cfg.DefaultScheme = IdentityConstants.ApplicationScheme;
        cfg.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
    })
    .AddJwtBearer()
    .AddCookie();


// Add services to the container.
//services.AddRazorPages();
//services.AddServerSideBlazor();

Caviar.Demo.Wasm.Program.PublicInit();
//客户端配置
services.AddCaviarServer();
services.AddAdminCaviar(new Type[] { typeof(Program), typeof(Caviar.Demo.Wasm.Program) });

//服务端配置
services.AddCaviar();
services.AddCaviarDbContext(options =>
//选择使用mysql或者sqlserver，其他的数据库只要efcore支持，本框架都支持
    options.UseSqlServer(
        configuration.GetConnectionString("DefaultConnection")
//options.UseMySQL(
//Configuration.GetConnectionString("DefaultConnection")
, b => b.MigrationsAssembly("Caviar.Demo.Hybrid")));
//跨域
services.AddCors(options =>
{
    options.AddPolicy("CorsPolicy",
        builder => builder.AllowAnyOrigin()
            .AllowAnyMethod()
            .AllowAnyHeader());
});
//控制器
services.AddControllers()
    .AddApplicationPart(typeof(Caviar.Infrastructure.API.ApplicationRoleController).Assembly);

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    //app.UseDeveloperExceptionPage();
    app.UseWebAssemblyDebugging();
}
else
{
    app.UseExceptionHandler("/Error", createScopeForErrors: true);
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseBlazorFrameworkFiles();

app.UseStaticFiles();
app.UseRouting();

app.UseCaviar();

app.UseAntiforgery();

app.MapControllers();

//app.MapFallbackToPage("/_Host");

app.MapRazorComponents<App>()
    .AddInteractiveServerRenderMode()
    //.AddInteractiveWebAssemblyRenderMode()
    .AddAdditionalAssemblies(Config.AdditionalAssemblies.ToArray())
    ;

app.Run();
